var path = require('path');
var webpack = require("webpack");
var merge = require('webpack-merge');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanPlugin = require('clean-webpack-plugin');

var T = process.env.npm_lifecycle_event === 'dev';

//定义了一些文件夹的路径
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'src/js/app.js');
var BUILD_PATH = path.resolve(ROOT_PATH, 'dist');
var plugins = [
      //将css成生文件，而非内联
      new ExtractTextPlugin("css/[name].[hash:5].css",{ 
          allChunks:true
      }),
      //
      new webpack.ProvidePlugin({
        $: "zepto",
        jQuery: "zepto",
        "window.jQuery": "zepto"
      }),
      //多个 html共用一个js文件(chunk)
      new webpack.optimize.CommonsChunkPlugin('js/common.[hash:5].js'),
      new HtmlWebpackPlugin({  // 生成html文件
        filename: 'index.html',
        template: "html-withimg-loader!"+path.resolve(ROOT_PATH, './src/index.html')
      })
];
var common = {
    // 入口文件
    entry: {main:APP_PATH},
    // 输出配置
    output: {
        // 输出路径是
        path: BUILD_PATH,
        publicPath: '',
        filename: 'js/[name].[hash:5].js'
    },
    resolve: {
        extensions: ['', '.js', '.css','scss'],//扩展后缀名
        //别名
        alias: { 
            'zepto'     : path.resolve(ROOT_PATH,'src/js/lib/zepto/1.1.7/zepto.min'),
            "tongji"    : path.resolve(ROOT_PATH,'src/js/lib/tongji/tongji'),
            'hobbit'    : path.resolve(ROOT_PATH,'src/js/lib/hobbit/hobbit'),
            'ui'        : path.resolve(ROOT_PATH,'src/js/lib/ui/hobbit.ui'),
            'ui-css'    : path.resolve(ROOT_PATH,'src/js/lib/ui/hobbit.ui.css')
      }
    },
    module: {
      loaders: [
          { test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
          {test:/\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader","sass-loader")},
         /* {test: /\.css$/, loader: "style!css"},*/
          {test: /\.scss$/, loader: "style!css!sass"},
          //图片文件使用 url-loader 来处理，小于8kb的直接转为base64
          //{test: /\.(jpg|png)$/, loader: "url?limit=2192"},//
          {
            test: /\.(jpg|jpe?g|png|gif|svg)$/,
            loader: 'url',
            query: {
              limit: 1000,
              name: 'imgs/[name].[hash:5].[ext]'
            },
            exclude: /node_modules/,
            include: path.resolve(__dirname, './src/imgs/')
          }
            ]
        
    },
    // 转化成es5的语法
    babel: {
      presets: ['es2015','stage-3'],
      plugins: ['transform-runtime']
    },
    plugins: plugins
}
if (T) {
  var config = merge(common, {
    devServer: {
      historyApiFallback: true,//代码热替换
      hot: true,
      inline: true,
      progress: true,
      stats: 'errors-only',
      host: process.env.HOST || 'localhost',
      port: process.env.PORT || '8080'
    },
    devtool: 'inline-source-map',
    plugins: [
    //new webpack.HotModuleReplacementPlugin()//代码热替换
    ]
  })

  config.entry.main = ['webpack/hot/dev-server', config.entry.main]
  module.exports = config
}else{
  module.exports = merge(common, {
    plugins:[
       //清理文件夹
       new CleanPlugin('dist'),
       //代码丑化
       new webpack.optimize.UglifyJsPlugin({
        compress: {
                      //supresses warnings, usually from module minification
                      warnings: false
                    },
                    minimize:true
                  })
       ]
     });
}
